<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  import serachGift from '@/components/serachGift/index.vue';
  import xmImage from '@/components/xmImage/index.vue';
  import { ElMessageBox } from 'element-plus';
  const formLabelWidth = '140px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogTableVisible:false,
    dialogFormVisible:false,
    disabled:false,
    gift_info:{},
    list:[] as Array<any>,
  })
  const listQuery = reactive({
    gift_id:'',
  })
  interface formQuert {
    git_id:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    git_id: [{ required: true, trigger: "blur", message: "请选择礼物池礼物" }],
  }
  const handleDel = (query:any={})=>{
    const { $index=-1, row={} } = query
    const { name='' } = row
    ElMessageBox.confirm(`是否删除礼物：${name}?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(() => {
      action.list.splice($index,1)
    })
    .catch(() => { });
  }
  const handleForm = (row:any={})=>{
    action.dialogFormVisible = true
  }
  /**
   * 新增礼物
   */
  const handleFormSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      action.list.push(action.gift_info)
      closeFormDialog()
    })
  }
  /**
   * 保存配置
   */
  const handleSubmit = ()=>{
    const loading = Toastloading()
    const ext_data = action.list.map(res=>{
      return res.gift_id
    })
    propApi.setSetboxGiftPool({
      gift_id:listQuery.gift_id,
      ext_data:JSON.stringify(ext_data)
    }).then(()=>{
      loading.close()
      ToastSuccess('配置成功')
      closeDialog()
    }).catch(()=>{
      loading.close()
    })
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await propApi.getSetboxGiftPool(listQuery)
    loading.close()
    const { data=[] } = res
    action.list = data
  }
  const openDialog = async(query:any = {})=> {
    const { gift_id=0, name='' } = query
    listQuery.gift_id = gift_id
    action.title = `${name}-礼物礼物池`
    const loading = Toastloading()
    await getList()
    loading.close()
    action.dialogTableVisible = true
  }
  const closeDialog = ()=>{
    action.dialogTableVisible = false
  }
  const closeFormDialog = ()=>{
    form = reactive({} as formQuert)
    action.gift_info = {}
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    v-model="action.dialogTableVisible"
    width="45%"
    :title="action.title">
      <div class="xm-headTab">
        <div class="labelBox">
          <div class="labelItem">
            <el-button type="primary" @click="getList">查询</el-button>
            <el-button type="primary" @click="handleForm">新增礼物池礼物</el-button>
          </div>
        </div>
      </div>
      <el-table
        class="xm-main xm-TableCenter"
        :data="action.list"
        border
      >
        <el-table-column width="80" prop="gift_id" label="礼物ID" />
        <el-table-column min-width="120" show-overflow-tooltip prop="name" label="礼物名称" />
        <el-table-column width="140" prop="image" label="礼物图片">
          <template #default="scoped">
            <xmImage :src="scoped.row.image" :list="[scoped.row.image]" />
          </template>
        </el-table-column>
        <el-table-column width="160" label="操作">
          <template #default="scoped">
            <el-button type="danger" @click="handleDel(scoped)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">保存配置</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="35%"
      title="新增礼物池礼物">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="礼物" prop="git_id">
          <serachGift :gift_type="1" v-model:gift_id="form.git_id" v-model:gift_info="action.gift_info" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeFormDialog">取 消</el-button>
          <el-button type="primary" @click="handleFormSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
